package com.houpu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import com.houpu.pojo.Department;
import com.houpu.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author miao
 * @date 2019/11/7  10:20
 */
@RestController
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    /**
     * 查询存在部门
     * @param page
     * @param limit
     * @param p_name
     * @param p_remark
     * @return
     */
    @RequestMapping(value = "layuidepartment", method = RequestMethod.GET)
    public Map<String, Object> layuilist(int page, int limit, String p_name, String p_remark) {
//        System.out.println(page + limit);
        // 从第page页开始 每页查询limit数据
        PageHelper.startPage(page, limit);
        Department department = new Department();
        if (!StringUtils.isEmpty(p_name)) {
            department.setP_name(p_name);
        }
        if (!StringUtil.isEmpty(p_remark)) {
            department.setP_remark(p_remark);
        }
        List<Department> list = departmentService.queryAll(department);
        PageInfo<Department> pageInfo = new PageInfo<>(list, limit);

        HashMap<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("data", pageInfo.getList());
        result.put("count", pageInfo.getTotal());
        return result;
    }

    /**
     * 恢复部门
     * @param page
     * @param limit
     * @param p_name
     * @param p_remark
     * @return
     */
    @RequestMapping(value = "selectPartor0", method = RequestMethod.GET)
    public Map<String, Object> selectPartor0(int page, int limit, String p_name, String p_remark) {
//        System.out.println(page + limit);
        // 从第page页开始 每页查询limit数据
        PageHelper.startPage(page, limit);
        Department department = new Department();
        if (!StringUtils.isEmpty(p_name)) {
            department.setP_name(p_name);
        }
        if (!StringUtil.isEmpty(p_remark)) {
            department.setP_remark(p_remark);
        }
        List<Department> list = departmentService.queryP_isor0(department);
        PageInfo<Department> pageInfo = new PageInfo<>(list, limit);

        HashMap<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("data", pageInfo.getList());
        result.put("count", pageInfo.getTotal());
        return result;
    }

    @RequestMapping(value = "returnPart", method = RequestMethod.POST)
    public Map<String, Object> returnPart(@RequestBody Department department) {
        System.out.println(department);

        //insert
        departmentService.updateP_isor1(department);

        HashMap<String, Object> result = new HashMap<>();
        result.put("status", "1");
        result.put("msg", "执行成功");
        return result;
    }


    @RequestMapping(value = "update", method = RequestMethod.POST)
    public Map<String, Object> update(@RequestBody Department department) {
        System.out.println(department);

        //insert
        departmentService.update(department);
        HashMap<String, Object> result = new HashMap<>();
        result.put("status", "2");
        result.put("msg", "执行成功");
        return result;
    }

    @RequestMapping(value = "updatePart", method = RequestMethod.POST)
    public Map<String, Object> delete(@RequestBody Department department) {
        System.out.println(department);

        //insert
        departmentService.delete(department);

        HashMap<String, Object> result = new HashMap<>();
        result.put("status", "1");
        result.put("msg", "执行成功");
        return result;
    }
//    @RequestMapping("insert.do")
//    public void getinsert() {
//        departmentService.insert();
//    }
    @RequestMapping(value = "layuidepartment", method = RequestMethod.POST)
    public Map<String, Object> add(@RequestBody Department department) {
        System.out.println(department);

        //insert
        departmentService.insert(department);

        HashMap<String, Object> result = new HashMap<>();
        result.put("status", "0");
        result.put("msg", "执行成功");
        return result;
    }
}
